Fix HTTP headers for issue attachment download#270
Conversation
- Download filename was wrong for files other than images. Example: It was `download` instead of `file.pdf` - PDF was downloading instead of showing on browser
| // Fix #312. Attachments with , in their name are not handled correctly by Google Chrome. | ||
| // We must put the name in " manually. | ||
| if err = repo.ServeData(ctx, "\""+attach.Name+"\"", fr); err != nil { | ||
| if err = repo.ServeData(ctx, attach.Name, fr); err != nil { |
There was a problem hiding this comment.
Does this re-introduce the referenced issue ?
gogs/gogs#312
There was a problem hiding this comment.
Did you test the issue explained in gogits#312 ?
I'm not sure the bug was related to the quotes in Content-Disposition rather than in repo.ServeData argument
There was a problem hiding this comment.
Hmm... In fact there's a problem, thanks for checking.
But I don't understand why it happen. I just moved the quotes, but it's there.
There was a problem hiding this comment.
@strk It's fixed now. It's was not actually fixed before, because the header was not actually set for these cases.
| ctx.Resp.Header().Set("Content-Transfer-Encoding", "binary") | ||
| } | ||
| } else if !ctx.QueryBool("render") { | ||
| ctx.Resp.Header().Set("Cache-Control", "public,max-age=86400") |
There was a problem hiding this comment.
This cache lifetime change seems to be unrelated to the scope of this PR
There was a problem hiding this comment.
The cache was there before, I just moved it to another place
There was a problem hiding this comment.
Ah, now I see, thanks for double-checking
| ctx.Resp.Header().Set("Cache-Control", "public,max-age=86400") | ||
|
|
||
| // Google Chrome dislike commas in filenames, so let's change it to a space | ||
| name = strings.Replace(name, ",", " ", -1) |
There was a problem hiding this comment.
I dislike spaces in filenames, how about a dash ? :)
Actually, how about a sanitizing function to generalize the issue ? Like, I'm thinking slashes might be impractical to have...
There was a problem hiding this comment.
Actually, how about a sanitizing function to generalize the issue ? Like, I'm thinking slashes might be impractical to have...
I did research on this and tried few approaches. Unfortunately there isn't how to sanitize it, it's actually a Chrome bug. And only commas trigger this AFAIK.
I don't have strong feeling for comma vs space.
|
underscores work for me too, although I prefer dashes as you can
do that without holding SHIFT :)
|
|
LGTM - the systems I use don't choke on spaces (it's just me, but I can blame the people who attach files with spaces or commas in their filenames) |
|
LGTM |
This fixes:
downloadinstead offile.pdf